package com.zorn.core.struts.interceptor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;

import org.apache.commons.io.IOUtils;

import static com.zorn.core.util.ParameterParser.parse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.zorn.core.provider.ActionSupportProvider;
import com.zorn.core.util.ParameterParser;

public class ParametersParseInterceptor implements Interceptor {

	private static final long serialVersionUID = 7281895201705003822L;

	@Override
	public void init() {

	}

	@Override
	public void destroy() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionSupportProvider action = (ActionSupportProvider) invocation.getAction();
		Map<String, Object> params = new HashMap<String, Object>(action.getRequest().getParameterMap());

		/*ServletInputStream is = action.getRequest().getInputStream();
		int len = 0;
		byte[] buffer = new byte[1024];
		StringBuffer sb = new StringBuffer();
		while ((len = is.read(buffer)) > 0) {
			sb.append(new String(buffer, 0, len, "UTF-8"));
		}
		String string = sb.toString();
		string.toCharArray();
		
		List<String> readLines = IOUtils.readLines(is, "UTF-8");
		readLines.size();
		String string2 = IOUtils.toString(is, "UTF-8");
		string2.toString();*/

		
		action.setParameterMap(ParameterParser.parse(params));

		return invocation.invoke();
	}

}
